home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4114 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.microsoft.com!news
  2. From: a-cnadc@microsoft.com (Dann Corbit)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Finding a prime number
  5. Date: 1 Feb 1996 18:09:09 GMT
  6. Organization: Microsoft Corporation
  7. Message-ID: <4eqvk5$7tn@news.microsoft.com>
  8. References: <4e875s$nqk@reader2.ix.netcom.com>
  9. NNTP-Posting-Host: 157.57.171.202
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.14
  12.  
  13. In article <4e875s$nqk@reader2.ix.netcom.com>, advtr@ix.netcom.co says...
  14. >
  15. >I need to write a function that will find wether or not a number is
  16. >prime.  I can come close but I get numbers that are not prime with the
  17. >prime numbers.
  18. >Here is the function I wrote.  Any help would be great.  Thanks  Ken(A
  19. >begining C programmer)
  20. >
  21. >int primenumber(int operand)
  22. >{/*Start primenumber*/
  23. >        int two = 0,three = 0,four = 0,five = 0,six = 0,seven = 0;
  24. >        int eight = 0,nine = 0;
  25. >        
  26. >
  27. >        two =(operand%2);
  28. >        three =(operand%3);
  29. >        four = (operand%4);
  30. >        five = (operand%5);
  31. >        six = (operand%6);
  32. >        seven = (operand%7);
  33. >        eight = (operand%8);
  34. >        nine = (operand%9);
  35. >
  36. >        if (two ==0||three==0||four==0||five==0||six==0
  37. >                ||seven==0||eight==0||nine==0)
  38. >                return 0;
  39. >        else
  40. >                return 1;
  41. >}/*End primenumber*/
  42. You only need to test division by primes (though the other tests
  43. don't hurt anything, they just make it slower).  You also quit
  44. testing too soon for any number bigger than 49.  If you want to
  45. use modulo testing, you should divide by all primes up to the 
  46. square root of the input number.
  47. -- 
  48. The opinions expressed in this message are my own personal views 
  49. and do not reflect the official views of Microsoft Corporation.
  50.  
  51.